'use strict'
const path = require('path')
const defaultSettings = require('./src/settings.js')
const CompressionPlugin = require('compression-webpack-plugin');

function resolve(dir) {
	return path.join(__dirname, dir)
}

// 在vue.config.js中configureWebpack中配置
// 要引入webpack
var webpack = require('webpack');

const {
	WEBAPI,
	PREAPI,
	PRMOCK,
	outputDir,
	PROXY
} = defaultSettings

const name = defaultSettings.title || '美莱企业管理后台' // page title
const port = 9528 // dev port
module.exports = {
	publicPath: './',
	outputDir,
	assetsDir: 'static',
	lintOnSave: false,
	productionSourceMap: false,
	devServer: {
		port: port,
		open: true,
		overlay: {
			warnings: false,
			errors: true
		},
		proxy: {
			[PREAPI]: {
				target: WEBAPI + PREAPI,
				changeOrigin: PROXY,
				pathRewrite: {
					['^' + PREAPI]: ''
				}
			},
			[PRMOCK]: {
				target: `http://localhost:${port}/mock`,
				changeOrigin: PROXY,
				pathRewrite: {
					['^' + PRMOCK]: ''
				}
			}
		},
		after: require('./mock/mock-server.js')
	},
	configureWebpack: {
		name: name,
		resolve: {
			alias: {
				'@': resolve('src')
			}
		},
		externals: {
			// 'vue': 'Vue',
			// 'vuex': 'Vuex',
			//'vue-router': 'VueRouter',
			// 'element-ui': 'ELEMENT',
			// 'Axios': 'axios'
		}
	},
	chainWebpack(config) {
		config.plugins.delete('preload')
		config.plugins.delete('prefetch')

		config.when(process.env.NODE_ENV === 'production', config => {
			config.optimization.minimize(true)
		})

		// set svg-sprite-loader
		config.module
			.rule('svg')
			.exclude.add(resolve('src/icons'))
			.end()
		config.module
			.rule('icons')
			.test(/\.svg$/)
			.include.add(resolve('src/icons'))
			.end()
			.use('svg-sprite-loader')
			.loader('svg-sprite-loader')
			.options({
				symbolId: 'icon-[name]'
			})
			.end()

		// set preserveWhitespace
		config.module
			.rule('vue')
			.use('vue-loader')
			.loader('vue-loader')
			.tap(options => {
				options.compilerOptions.preserveWhitespace = true
				return options
			})
			.end()

		config
			.when(process.env.NODE_ENV === 'development', config =>
				config.devtool('source-map')
			)

		config.when(process.env.NODE_ENV !== 'development', config => {
			config
				.plugin('ScriptExtHtmlWebpackPlugin')
				.after('html')
				.use('script-ext-html-webpack-plugin', [{
					inline: /runtime\..*\.js$/
				}])
				.end()
			config.optimization.splitChunks({
				chunks: 'all',
				cacheGroups: {
					libs: {
						name: 'chunk-libs',
						test: /[\\/]node_modules[\\/]/,
						priority: 10,
						chunks: 'initial' // only package third parties that are initially dependent
					},
					elementUI: {
						name: 'chunk-elementUI', // split elementUI into a single package
						priority: 20, // the weight needs to be larger than libs and app or it will be packaged into libs or app
						test: /[\\/]node_modules[\\/]_?element-ui(.*)/ // in order to adapt to cnpm
					},
					commons: {
						name: 'chunk-commons',
						test: resolve('src/components'), // can customize your rules
						minChunks: 3, //  minimum common number
						priority: 5,
						reuseExistingChunk: true
					}
				}
			})
			config.optimization.runtimeChunk('single')
			if (process.env.NODE_ENV === 'production') {
				config.plugin('compressionPlugin')
					.use(new CompressionPlugin({
						filename: '[path].gz[query]',
						algorithm: 'gzip',
						test: /\.(js|css|json|txt|html|ico|svg)(\?.*)?$/i,
						threshold: 10240,
						minRatio: 0.8,
						deleteOriginalAssets: false
					}));
			}

		})

		// quill
		config.plugin('provide').use(webpack.ProvidePlugin, [{
			'window.Quill': 'quill/dist/quill.js',
			'Quill': 'quill/dist/quill.js'
		}]);
		
		config.externals({ './cptable': 'var cptable' });
	},
	css: {
		loaderOptions: {
			// 给 sass-loader 传递选项
			sass: {
				data: `@import "./src/styles/base.scss";`
			}
		}
	}
}
